package com.he1618;

import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.Receiver;
import org.jgroups.View;

public class SimpleJGroupsExample implements Receiver {
    private JChannel channel;

    public void viewAccepted(View view) {
        System.out.println("View: " + view);
    }

    public void receive(Message msg) {
        System.out.println("Received message: " + msg.getObject());
    }

    public void start() throws Exception {
        channel = new JChannel("udp.xml");
        channel.setReceiver(this);
        channel.connect("demo-cluster");

        eventLoop();
        //channel.close();
    }

    private void eventLoop() {
        // 在这里编写你的业务逻辑和消息发送代码
        // 使用channel.send(...)发送消息
    }

    public static void main(String[] args) {
        try {
            new SimpleJGroupsExample().start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
